// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.vehicles;

import fmod.fmod.FMODSoundEmitter;
import org.joml.Vector3f;
import zombie.ai.State;
import zombie.ai.states.ZombieIdleState;
import zombie.audio.BaseSoundEmitter;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.network.GameServer;

public final class AttackVehicleState extends State {
    private static final AttackVehicleState _instance = new AttackVehicleState();
    private BaseSoundEmitter emitter;
    private final Vector3f worldPos = new Vector3f();

    public static AttackVehicleState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        if (isoZombie.target instanceof IsoGameCharacter isoGameCharacter) {
            if (isoGameCharacter.isDead()) {
                if (isoGameCharacter.getLeaveBodyTimedown() > 3600.0F) {
                    isoZombie.changeState(ZombieIdleState.instance());
                    isoZombie.setTarget(null);
                } else {
                    isoGameCharacter.setLeaveBodyTimedown(isoGameCharacter.getLeaveBodyTimedown() + zombie.GameTime.getInstance().getMultiplier() / 1.6F);
                    if (!GameServer.bServer && !Core.SoundDisabled && Rand.Next(Rand.AdjustForFramerate(15)) == 0) {
                        if (this.emitter == null) {
                            this.emitter = new FMODSoundEmitter();
                        }

                        String string = isoZombie.isFemale() ? "FemaleZombieEating" : "MaleZombieEating";
                        if (!this.emitter.isPlaying(string)) {
                            this.emitter.playSound(string);
                        }
                    }
                }

                isoZombie.TimeSinceSeenFlesh = 0.0F;
            } else {
                BaseVehicle baseVehicle = isoGameCharacter.getVehicle();
                if (baseVehicle != null && baseVehicle.isCharacterAdjacentTo(owner)) {
                    Vector3f vector3f = baseVehicle.chooseBestAttackPosition(isoGameCharacter, owner, this.worldPos);
                    if (vector3f == null) {
                        if (isoZombie.AllowRepathDelay <= 0.0F) {
                            owner.pathToCharacter(isoGameCharacter);
                            isoZombie.AllowRepathDelay = 6.25F;
                        }
                    } else if (vector3f != null && (Math.abs(vector3f.x - owner.x) > 0.1F || Math.abs(vector3f.y - owner.y) > 0.1F)) {
                        if (!(Math.abs(baseVehicle.getCurrentSpeedKmHour()) > 0.8F)
                            || !baseVehicle.isCharacterAdjacentTo(owner) && !(baseVehicle.DistToSquared(owner) < 16.0F)) {
                            if (isoZombie.AllowRepathDelay <= 0.0F) {
                                owner.pathToCharacter(isoGameCharacter);
                                isoZombie.AllowRepathDelay = 6.25F;
                            }
                        }
                    } else {
                        owner.faceThisObject(isoGameCharacter);
                    }
                }
            }
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
        IsoZombie isoZombie = (IsoZombie)owner;
        if (isoZombie.target instanceof IsoGameCharacter isoGameCharacter) {
            BaseVehicle baseVehicle = isoGameCharacter.getVehicle();
            if (baseVehicle != null) {
                if (!isoGameCharacter.isDead()) {
                    if (event.m_EventName.equalsIgnoreCase("AttackCollisionCheck")) {
                        isoGameCharacter.getBodyDamage().AddRandomDamageFromZombie(isoZombie, null);
                        isoGameCharacter.getBodyDamage().Update();
                        if (isoGameCharacter.isDead()) {
                            if (isoGameCharacter.isFemale()) {
                                isoZombie.getEmitter().playVocals("FemaleBeingEatenDeath");
                            } else {
                                isoZombie.getEmitter().playVocals("MaleBeingEatenDeath");
                            }

                            isoGameCharacter.setHealth(0.0F);
                        } else if (isoGameCharacter.isAsleep()) {
                            if (GameServer.bServer) {
                                isoGameCharacter.sendObjectChange("wakeUp");
                                isoGameCharacter.setAsleep(false);
                            } else {
                                isoGameCharacter.forceAwake();
                            }
                        }
                    } else if (event.m_EventName.equalsIgnoreCase("ThumpFrame")) {
                        VehicleWindow vehicleWindow = null;
                        VehiclePart vehiclePart0 = null;
                        int _int = baseVehicle.getSeat(isoGameCharacter);
                        String string = baseVehicle.getPassengerArea(_int);
                        if (baseVehicle.isInArea(string, owner)) {
                            VehiclePart vehiclePart1 = baseVehicle.getPassengerDoor(_int);
                            if (vehiclePart1 != null
                                && vehiclePart1.getDoor() != null
                                && vehiclePart1.getInventoryItem() != null
                                && !vehiclePart1.getDoor().isOpen()) {
                                vehicleWindow = vehiclePart1.findWindow();
                                if (vehicleWindow != null && !vehicleWindow.isHittable()) {
                                    vehicleWindow = null;
                                }

                                if (vehicleWindow == null) {
                                    vehiclePart0 = vehiclePart1;
                                }
                            }
                        } else {
                            vehiclePart0 = baseVehicle.getNearestBodyworkPart(owner);
                            if (vehiclePart0 != null) {
                                vehicleWindow = vehiclePart0.getWindow();
                                if (vehicleWindow == null) {
                                    vehicleWindow = vehiclePart0.findWindow();
                                }

                                if (vehicleWindow != null && !vehicleWindow.isHittable()) {
                                    vehicleWindow = null;
                                }

                                if (vehicleWindow != null) {
                                    vehiclePart0 = null;
                                }
                            }
                        }

                        if (vehicleWindow != null) {
                            vehicleWindow.damage(isoZombie.strength);
                            baseVehicle.setBloodIntensity(vehicleWindow.part.getId(), baseVehicle.getBloodIntensity(vehicleWindow.part.getId()) + 0.025F);
                            if (!GameServer.bServer) {
                                isoZombie.setVehicleHitLocation(baseVehicle);
                                owner.getEmitter().playSound("ZombieThumpVehicleWindow", baseVehicle);
                            }

                            isoZombie.setThumpFlag(3);
                        } else {
                            if (!GameServer.bServer) {
                                isoZombie.setVehicleHitLocation(baseVehicle);
                                owner.getEmitter().playSound("ZombieThumpVehicle", baseVehicle);
                            }

                            isoZombie.setThumpFlag(1);
                        }

                        baseVehicle.setAddThumpWorldSound(true);
                        if (vehiclePart0 != null && vehiclePart0.getWindow() == null && vehiclePart0.getCondition() > 0) {
                            vehiclePart0.setCondition(vehiclePart0.getCondition() - isoZombie.strength);
                            vehiclePart0.doInventoryItemStats(vehiclePart0.getInventoryItem(), 0);
                            baseVehicle.transmitPartCondition(vehiclePart0);
                        }

                        if (isoGameCharacter.isAsleep()) {
                            if (GameServer.bServer) {
                                isoGameCharacter.sendObjectChange("wakeUp");
                                isoGameCharacter.setAsleep(false);
                            } else {
                                isoGameCharacter.forceAwake();
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public boolean isAttacking(IsoGameCharacter owner) {
        return true;
    }

    public boolean isPassengerExposed(IsoGameCharacter owner) {
        if (!(owner instanceof IsoZombie isoZombie)) {
            return false;
        } else if (!(isoZombie.target instanceof IsoGameCharacter isoGameCharacter)) {
            return false;
        } else {
            BaseVehicle baseVehicle = isoGameCharacter.getVehicle();
            if (baseVehicle == null) {
                return false;
            } else {
                boolean _boolean = false;
                Object object = null;
                int _int = baseVehicle.getSeat(isoGameCharacter);
                String string = baseVehicle.getPassengerArea(_int);
                VehiclePart vehiclePart = null;
                if (baseVehicle.isInArea(string, owner)) {
                    vehiclePart = baseVehicle.getPassengerDoor(_int);
                    if (vehiclePart != null && vehiclePart.getDoor() != null) {
                        if (vehiclePart.getInventoryItem() != null && !vehiclePart.getDoor().isOpen()) {
                            object = vehiclePart.findWindow();
                            if (object != null) {
                                if (!((VehicleWindow)object).isHittable()) {
                                    object = null;
                                }

                                _boolean = object == null;
                            } else {
                                _boolean = false;
                            }
                        } else {
                            _boolean = true;
                        }
                    }
                } else {
                    vehiclePart = baseVehicle.getNearestBodyworkPart(owner);
                    if (vehiclePart != null) {
                        object = vehiclePart.findWindow();
                        if (object != null && !((VehicleWindow)object).isHittable()) {
                            object = null;
                        }
                    }
                }

                return _boolean;
            }
        }
    }
}
